<?php
namespace Admin\Controller;
class SettingController extends CommonController {
    public function _initialize() {
        parent::_initialize();
    }
    public function index() {
        $type = I('type', 'index', 'trim');
        $name = I('name','site','trim');
        $where = array('name'=>$name);
        $list = M('setting')->where($where)->find();
        $info = json_decode($list['data'],true);
        $this->assign("info",$info);
        $this->display($type);
    }
	//图片水印
	public function watermark(){
		if(IS_POST){
		$config_file = './App/Admin/Conf/water.php';
        //$config = require $config_file;
		$new_config = array(
				'WATER_FUNTION'=>I('WATER_FUNTION','intval'),	//是否开启水印功能
				'WATER_FILE_TYPE'=>I('WATER_FILE_TYPE','intval'),	//选择水印的类型
                'WATER_IMAGE' => $_POST['img'] ? $_POST['img'] : C('WATER_IMAGE'),	//图片水印路径
				'WATER_POS'=> I('WATER_POS','intval'),//水印位置
				'WATER_ALPHA'=> I('WATER_ALPHA','intval'),	//水印透明度
				'WATER_COMPRESSION'=>80,	//JPEG图片压缩比
				'WATER_TEXT'=> I('WATER_TEXT','trim'),	//水印文字
				'WATER_ANGLE'=> I('WATER_ANGLE','intval'),	//水印文字旋转角度
				'WATER_FONTSIZE'=> I('WATER_FONTSIZE','intval'),	//字体大小
				'WATER_FONTCOLOR'=> I('WATER_FONTCOLOR'),	//水印字体颜色
				'WATER_FONTFILE'=>'./data/water/font.ttf',	//水印字体
				'WATER_CHARSET'=>'UTF-8',	//水印文字编码
            );        
		   if (update_config($new_config, $config_file)) {
                $this->success('修改成功');
            } else {
                $this->error(L('修改失败'));
            }
		}else{
            $this->display();
		}
	}

    public function edit() {
        if(IS_POST){
            $config = M('setting');
            $info = $_POST['info'];
            $name = array_keys($info);
            $values = array_values($info);
            $name = $name[0];
            $value = json_encode($values[0]);
            $where = array('name'=>$name);
            if($config->where($where)->find()){
                if($config->where($where)->setField('data', $value)){
                    $this->success('保存成功！');
                }else{
                    $this->error('保存失败！');
                }
            }else{
               if($config->add(array('name'=>$name,'data'=>$value))){
                   $this->success('保存成功！');
               }else{
                   $this->error('保存失败！');
               }
            }
        }
    }

    //logo图片处理
    public function logo() {
        if(IS_POST){
            $logo = array();
            foreach($_POST['logo'] as $k=>$v){
                if($v){
                    $old_name = '.'.$v;
                    $p = pathinfo($old_name);
                    $new_name = "./Uploads/Logo/".$k.".".$p['extension'];
                    @rename($old_name,$new_name);
                    $logo[$k] = $new_name ? ltrim($new_name,'.') : $v;
                }
            }
            $logo['time'] = time();
            if(M('setting')->where(array('name'=>'logo'))->setField(array('data'=>json_encode($logo)))){
                $this->success('保存成功！');
            }else{
                $this->error('保存失败！');
            }
        }else{
            $where = array('name'=>'logo');
            $list = M('setting')->where($where)->find();
            $info = json_decode($list['data'],true);
            $this->assign("info",$info);
            $this->display();
        }
    }

}